package com.flea.hclv.controller;

import com.flea.hclv.util.AesCbcUtil;
import com.flea.hclv.util.HttpRequest;
import com.flea.hclv.util.ServerResponse;
import com.flea.hclv.util.share.WeinXinUtil;
import com.flea.hclv.util.share.WinXinEntity;

import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

/**
 * @program: project
 * @description: 微信分享
 * @author: shiliqiang
 * @create: 2018-12-13 10:53
 * @version: 1.0
 */
@RequestMapping("/flea/wx/")
@Controller
public class WxShareController {


    @RequestMapping(value = "share",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<WinXinEntity> share(HttpServletRequest request){
//        JSONArray jsonArray= JSON.parseArray(url);
//        String url1= (String)jsonArray.get(0);
        //微信分享
        String strUrl = "http://www.imflea.cn/hclv-web"
                + request.getContextPath()   //项目名称
                + request.getServletPath()   //请求页面或其他地址
                + "?" + (request.getQueryString()); //参数
        WinXinEntity wx = WeinXinUtil.getWinXinEntity(strUrl);
        return ServerResponse.createBySuccess(wx);
    }
    @RequestMapping(value = "/decodeUserInfo", method = RequestMethod.POST)
	@ResponseBody
    public Map<String, Object> decodeUserInfo(String encryptedData, String iv, String code) {
		Map map = new HashMap<String, Object>();
		// 登录凭证不能为空
		if (code == null || code.length() == 0) {
			map.put("status", 0);
			map.put("msg", "code 不能为空");
			return map;
		}
 
		// 小程序唯一标识 (在微信小程序管理后台获取)
		String wxspAppid = "wx2164d8a22034b025";
		// 小程序的 app secret (在微信小程序管理后台获取)
		String wxspSecret = "cf1b380f49391930900edb870ca21079";
		// 授权（必填）
		String grant_type = "authorization_code";
 
		//////////////// 1、向微信服务器 使用登录凭证 code 获取 session_key 和 openid
		//////////////// ////////////////
		// 请求参数
		String params = "appid=" + wxspAppid + "&secret=" + wxspSecret + "&js_code=" + code + "&grant_type="
				+ grant_type;
		// 发送请求
		String sr = HttpRequest.sendGet("https://api.weixin.qq.com/sns/jscode2session", params);
		// 解析相应内容（转换成json对象）
		JSONObject json = new JSONObject(sr);
		// 获取会话密钥（session_key）
		String session_key = json.get("session_key").toString();
		// 用户的唯一标识（openid）
		String openid = (String) json.get("openid");
 
		//////////////// 2、对encryptedData加密数据进行AES解密 ////////////////
		try {
			String result = AesCbcUtil.decrypt(encryptedData, session_key, iv, "UTF-8");
			if (null != result && result.length() > 0) {
				map.put("status", 1);
				map.put("msg", "解密成功");
				map.put("openid", openid);
				
 
				JSONObject userInfoJSON = new JSONObject(result);
				Map userInfo = new HashMap();
				userInfo.put("openId", userInfoJSON.get("openId"));
				userInfo.put("nickName", userInfoJSON.get("nickName"));
				userInfo.put("gender", userInfoJSON.get("gender"));
				userInfo.put("city", userInfoJSON.get("city"));
				userInfo.put("province", userInfoJSON.get("province"));
				userInfo.put("country", userInfoJSON.get("country"));
				userInfo.put("avatarUrl", userInfoJSON.get("avatarUrl"));
				// 解密unionId & openId;
 
				userInfo.put("unionId", userInfoJSON.get("unionId"));
				map.put("userInfo", userInfo);
				map.put("openId", userInfoJSON.get("openId"));
			} else {
				map.put("status", 0);
				map.put("msg", "解密失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}
}
